package com.koron.car.web.authorized.service.impl;

import com.koron.bean.system.staff.vo.EamUser;
import com.koron.car.config.Constants;
import com.koron.car.web.authorized.bean.entity.OperateControl;
import com.koron.car.web.authorized.bean.entity.Revenue;
import com.koron.car.web.authorized.bean.query.OperateControlQuery;
import com.koron.car.web.authorized.bean.vo.OperateControlVO;
import com.koron.car.web.authorized.mapper.OperateControlMapper;
import com.koron.car.web.authorized.service.OperateControlService;
import com.koron.car.web.authorized.service.RevenueService;
import com.koron.car.web.carledger.service.CarService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * (OperateControl)表服务实现类
 *
 * @author twcao
 * @since 2022-03-30 16:48:32
 */
@Service
public class OperateControlServiceImpl implements OperateControlService {

    @Autowired
    private RevenueService revenueService;

    @Autowired
    private CarService carService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public OperateControlVO queryById(SessionFactory factory, String id) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByApplyId")
    public OperateControlVO queryByApplyId(SessionFactory factory, String applyId) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        return mapper.queryByApplyId(applyId);
    }

    @Override
    @TaskAnnotation("queryByMark")
    public OperateControlVO queryByMark(SessionFactory factory) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        OperateControlVO vo = mapper.queryByMark(factory.getDbEnv());
        int managerNum = carService.countCarByPrepareType(factory, Constants.PrepareType.DELIMIT_MANAGER);
        int securityNum = carService.countCarByPrepareType(factory, Constants.PrepareType.DELIMIT_SECURITY);
        if(vo == null){
           vo = new OperateControlVO();
        }
        vo.setFactManagerNum(managerNum);
        vo.setFactSecurityNum(securityNum);
        vo.setFactNum(vo.getFactManagerNum() + vo.getFactSecurityNum());
        return vo;
    }

    @Override
    @TaskAnnotation("saveOrUpdate")
    public OperateControlVO saveOrUpdate(SessionFactory factory, OperateControlVO oc) {
        Assert.notNull(oc, "概况不能为空");
        // 每次修改都形成历史记录
        OperateControlVO old = queryByMark(factory);
        if (com.koron.common.core.util.StringUtils.isNotNull(old)) {
            oc.setDelimitManagerNum(old.getDelimitManagerNum());
            oc.setDelimitSecurityNum(old.getDelimitSecurityNum());
            oc.setDelimitEffectTime(old.getDelimitEffectTime());
            oc.setFactManagerNum(old.getFactManagerNum());
            oc.setFactSecurityNum(old.getFactSecurityNum());
            oc.setFactNum(old.getFactNum());
            oc.setMark(factory.getDbEnv());
            // 将新传入的，覆盖原先的
            BeanUtils.copyProperties(oc, old);
            old.setId(null);
        }
        // 永远都是新增，因为要存历史记录
        return old == null || StringUtils.isBlank(old.getId()) ? insert(factory, old) : update(factory, old);
    }

    /**
     * 新增数据
     * @param oc 实例对象
     * @return 实例对象
     */
    @Override
    public OperateControlVO insert(SessionFactory factory, OperateControlVO oc) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        if (Objects.isNull(oc)){
            oc = new OperateControlVO();
        }
        oc.setId(CodeTools.getCode32());
        EamUser eamUser = ThreadLocalContext.get();
        oc.setCreateBy(eamUser.getAccount());
        oc.setCreateByName(eamUser.getName());
        mapper.insert(oc);
        List<Revenue> revenues = oc.getRevenues();
        if(CollectionUtils.isNotEmpty(revenues)) {
            OperateControlVO finalOc = oc;
            revenues.stream().forEach(row -> row.setOperateControlId(finalOc.getId()));
            revenueService.insertBatch(factory, revenues);
        }
        return oc;
    }

    /**
     * 修改数据
     * @param oc 实例对象
     * @return 实例对象
     */
    @Override
    public OperateControlVO update(SessionFactory factory, OperateControlVO oc) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        mapper.update(oc);
        List<Revenue> revenues = oc.getRevenues();
        if(CollectionUtils.isNotEmpty(revenues)) {
            revenueService.deleteById(factory, oc.getId());
            revenues.stream().forEach(row -> row.setOperateControlId(oc.getId()));
            revenueService.insertBatch(factory, revenues);
        }
        return queryById(factory, oc.getId());
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OperateControl> queryList(SessionFactory factory, OperateControlQuery query) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        return mapper.queryList(query);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        OperateControlMapper mapper = factory.getMapper(OperateControlMapper.class);
        return mapper.deleteById(id) > 0;
    }
}
